package com.saturn.titan.web.controller.appinterface;

import java.text.ParseException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.mapper.JsonMapper;

import com.google.common.collect.Maps;
import com.saturn.titan.core.comm.Constants;
import com.saturn.titan.core.comm.ResponseBean;
import com.saturn.titan.core.dto.ConfermWorkBillDto;
import com.saturn.titan.core.dto.FinishBillDto;
import com.saturn.titan.core.service.RepairAndInstallBillService;
import com.saturn.titan.core.service.WorkBillService;
import com.saturn.titan.web.controller.BaseController;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/app")
public class AppController extends BaseController{

	private static JsonMapper mapper = new JsonMapper();
	
	@Autowired
	private WorkBillService workBillService;
	
	@Autowired
	private RepairAndInstallBillService repairAndInstallBillService;
	
	@RequestMapping(value = "getAllWork", method = RequestMethod.GET)
	@ResponseBody
	public String getAllWork(@RequestParam(value="empId") Long empId,@RequestParam(value="billStt") Integer billStt) throws ParseException {
		Object result = null;
		if(billStt.intValue() == 0){
			//获取待接收
			result = workBillService.getAllWork(empId);
		}else if(billStt.intValue() == 1){
			//获取已接收
			result = workBillService.getReciveWork(empId);
		}
		Map<String,Object> map = Maps.newHashMap();
		map.put("list", result);
		return mapper.toJson(map);
		
	}
	
	@RequestMapping(value = "getReciveWork", method = RequestMethod.GET)
	@ResponseBody
	public String getReciveWork(@RequestParam(value="empId") Long empId) throws ParseException {
		
		Object result = workBillService.getReciveWork(empId);
		Map<String,Object> map = Maps.newHashMap();
		map.put("list", result);
		return mapper.toJson(map);
		
	}
	
	@RequestMapping(value = "finishOrder", method = RequestMethod.POST)
	@ResponseBody
	public String finishOrder(@RequestParam(value="json")String json) throws ParseException {
		ResponseBean b = new ResponseBean();
		
		try {
			FinishBillDto dto = mapper.fromJson(json, FinishBillDto.class);
			if(dto == null){
				throw new Exception("请求格式非法");
			}
			if(dto.getBillId() == null){
				throw new Exception("维修单或安装单Id不能为空");
			}
			if(dto.getEmpId() == null){
				throw new Exception("员工不能为空");
			}
			if(dto.getType() == null){
				throw new Exception("工单类型为空");
			}
			repairAndInstallBillService.finishOrder(dto);
			b.setResult("OK");
		} catch (Exception e) {
			e.printStackTrace();
			b.setResult("NG");
			b.setMeg(e.getMessage());
		}
		return mapper.toJson(b);
	}
	
	
	@RequestMapping(value = "confirmOrder", method = RequestMethod.POST)
	@ResponseBody
	public String confirmOrder(@RequestParam(value="json")String json) throws ParseException {
		ResponseBean b = new ResponseBean();
		
		try {
			ConfermWorkBillDto dto = mapper.fromJson(json, ConfermWorkBillDto.class);
			if(dto == null){
				throw new Exception("请求格式非法");
			}
			if(dto.getType() == null){
				throw new Exception("工单类型为空");
			}
			if(dto.getBillId() == null){
				throw new Exception("维修单或安装单Id不能为空");
			}
			if(dto.getEmpId() == null){
				throw new Exception("员工不能为空");
			}
			if(dto.getBillStt() == null){
				throw new Exception("操作类型不能为空");
			}
			
			//确认接收
			if(dto.getBillStt().intValue() == 1){
				if(Constants.REQUEST_TYPE_INSTALL.equals(dto.getType())){
					repairAndInstallBillService.confirmInstall(dto);
				}else{
					repairAndInstallBillService.confirmRepair(dto);
				}
			}
			//延迟单据
			else if(dto.getBillStt().intValue() == 2){
				//TODO
			}
			//完成单据
			else if(dto.getBillStt().intValue() == 3){
				//TODO
			}
			//作废
			else if(dto.getBillStt().intValue() == 9){
				//TODO
			}else{
				throw new Exception("非法的操作类型");
			}
			b.setResult("OK");
			b.setMeg("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			b.setResult("NG");
			b.setMeg(e.getMessage());
		}
		return mapper.toJson(b);
	}
	
	@RequestMapping(value = "test", method = RequestMethod.GET)
	public String test() throws ParseException {
		return "test/appTest";
	}
	
}
